package com.java.base.exercise.current.threadpool;

import java.util.concurrent.*;

/**
 * @Author RenPu
 * @Date 2021/9/29 10:35
 * @Version 1.0
 * @Description: 单线程的线程池，能保证线程按照提交的顺序进行执行任务  场景模拟
 **/
public class SingleThreadPoolDemo {
    public static void main(String[] args) {


        ExecutorService executorService = Executors.newSingleThreadExecutor();


        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2, 0, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(66665));

        //预创建线程池的线程，根据核心线程数的参数进行创建或者一个线程
        threadPoolExecutor.prestartAllCoreThreads();
        threadPoolExecutor.prestartCoreThread();

        for (int i = 0; i < 10; i++) {

            final int no = i;

            Runnable runnable = new Runnable() {

                @Override
                public void run() {

                    while (true){
                        try {
                            System.out.println(no + " into ! !");
                            TimeUnit.SECONDS.sleep(2);
                            System.out.println(no + "end ! !");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            executorService.execute(runnable);
        }

        System.out.println("Main Thread End !!!!!!");



        executorService.shutdown();
    }


}
